<script lang="ts">
export default {
  name: "BarDevelop"
};
</script>

<script setup lang="ts">
import { ECharts } from "echarts";
import echarts from "/@/plugins/echarts";
import { onMounted, nextTick, ref } from "vue";
import { tryOnUnmounted, useTimeoutFn } from "@vueuse/core";
import { postProjectBill } from "/@/api/analysis";

import {
  addResizeListener,
  removeResizeListener
} from "element-plus/lib/utils/resize-event";

let echartInstance: ECharts;

const barDevelop = ref<HTMLElement | null>(null) as any;

let xAxisData = ref([]);
let seriesData = ref([]);

function initechartInstance(xAxisData, seriesData) {
  const echartDom = document.querySelector(".barDevelop");
  if (!echartDom) return;
  // @ts-ignore
  echartInstance = echarts.init(echartDom);
  echartInstance.clear(); //清除旧画布 重新渲染

  echartInstance.setOption({
    color: ["#59a0ff", "#00e6ad", "#ffc500"],
    tooltip: {
      trigger: "axis",
      axisPointer: {
        type: "shadow"
      }
    },
    legend: {
      // orient: "vertical",
      right: true
    },
    grid: {
      // bottom: "20%",
      top: 10,
      right: 20,
      bottom: 20,
      width: "100%",
      containLabel: true
    },
    yAxis: [
      {
        type: "category",
        axisTick: {
          alignWithLabel: true
        },

        data: xAxisData
        // ["贵州", "济南", "工地视频"]
      }
    ],
    xAxis: [
      {
        type: "value"
      }
    ],
    series: seriesData
    //  [
    //   {
    //     type: "bar",
    //     data: [204, 600, 1079]
    //   }
    // ]
  });
}

const initProjectNum = async () => {
  try {
    const { code, data } = await postProjectBill({});
    if (code === 200) {
      xAxisData.value = data.xAxisData;

      seriesData.value = [
        {
          type: "bar",
          data: data.seriesData,
          label: {
            show: true,
            position: "right"
          }
        }
      ];
      seriesData.value[0].data = seriesData.value[0].data.reverse();

      nextTick(() => {
        initechartInstance(xAxisData.value.reverse(), seriesData.value);
      });
    }
  } catch (err) {
    console.log(err);
  }
};

initProjectNum();

onMounted(() => {
  nextTick(() => {
    addResizeListener(barDevelop.value, resetRender);
  });
  // useEventListener("resize", () => {
  //   console.log("resize");
  //   if (!echartInstance) return;
  //   useTimeoutFn(() => {
  //     echartInstance.resize();
  //   }, 180);
  // });
});

const resetRender = () => {
  if (!echartInstance) return;
  useTimeoutFn(() => {
    echartInstance.resize();
  }, 180);
};

tryOnUnmounted(() => {
  if (!echartInstance) return;
  echartInstance.dispose();
  echartInstance = null;
  removeResizeListener(barDevelop.value, resetRender);
});
</script>

<template>
  <div ref="barDevelop">
    <div class="title">
      <slot> </slot>
    </div>
    <div class="barDevelop"></div>
  </div>
</template>

<style lang="scss" scoped>
.title {
  font-size: 16px;
  font-weight: bold;
  padding-bottom: 10px;
}
.barDevelop {
  width: 100%;
  height: 282px;
}
</style>
